package com.lizonghui.googleplay.control;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lizonghui.googleplay.R;
import com.lizonghui.googleplay.base.BaseHolder;
import com.lizonghui.googleplay.bean.CategoryInfoBean;
import com.lizonghui.googleplay.config.Constants;
import com.lizonghui.googleplay.utils.UIUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Administrator on 2016/6/23.
 */
public class CategoryNormalHolder extends BaseHolder<CategoryInfoBean> {

    @Bind(R.id.item_category_icon_1)
    ImageView mItemCategoryIcon1;
    @Bind(R.id.item_category_name_1)
    TextView mItemCategoryName1;
    @Bind(R.id.item_category_item_1)
    LinearLayout mItemCategoryItem1;
    @Bind(R.id.item_category_icon_2)
    ImageView mItemCategoryIcon2;
    @Bind(R.id.item_category_name_2)
    TextView mItemCategoryName2;
    @Bind(R.id.item_category_item_2)
    LinearLayout mItemCategoryItem2;
    @Bind(R.id.item_category_icon_3)
    ImageView mItemCategoryIcon3;
    @Bind(R.id.item_category_name_3)
    TextView mItemCategoryName3;
    @Bind(R.id.item_category_item_3)
    LinearLayout mItemCategoryItem3;


    @Override
    public void refreshHolderView(CategoryInfoBean data) {
       /* mItemCategoryName1.setText(data.name1);
        mItemCategoryName2.setText(data.name2);
        mItemCategoryName3.setText(data.name3);

        //图片绑定用Picasso
        Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL+data.url1).into(mItemCategoryIcon1);
        Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL+data.url2).into(mItemCategoryIcon2);
        Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL+data.url3).into(mItemCategoryIcon3);
*/
        initData(data.name1,data.url1,mItemCategoryIcon1,mItemCategoryName1);
        initData(data.name2,data.url2,mItemCategoryIcon2,mItemCategoryName2);
        initData(data.name3,data.url3,mItemCategoryIcon3,mItemCategoryName3);
    }

    @Override
    public View initHolderView() {
        View rootView = View.inflate(UIUtils.getContext(), R.layout.item_category_normal, null);

        ButterKnife.bind(this, rootView);
        return rootView;
    }

    public void initData(final String name,String url,ImageView iv,TextView tv){
        if(!TextUtils.isEmpty(name)  && !TextUtils.isEmpty(url)){
            tv.setText(name);
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL+url).into(iv);


            //点击事件只需要写一次
            ViewParent parent = tv.getParent();
            //要找到父控件，并且转为ViewGroup模式才可以设置点击事件
            ((ViewGroup)parent).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(UIUtils.getContext(), name, Toast.LENGTH_SHORT).show();
                }
            });
            ((ViewGroup) parent).setVisibility(View.VISIBLE);
        }else {
            //当么有数据的时候，父容器设置为空
            ViewParent parent = tv.getParent();
            ((ViewGroup)parent).setVisibility(View.INVISIBLE);

        }

    }

}
